package cn.gson.school.controller.jiaoxue;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.gson.school.dao.jiaoxue.ClassDao;
import cn.gson.school.dao.jiaoxue.StudentDao;
import cn.gson.school.dao.xitong.UserInfoDao;
import cn.gson.school.entity.jiaoxue.ChapterEntity;
import cn.gson.school.entity.jiaoxue.ClassEntity;
import cn.gson.school.entity.jiaoxue.StudentEntity;
import cn.gson.school.mappers.jiaoxue.ClassMappers;
import cn.gson.school.service.jiaoxue.StudentService;

@Controller
@RequestMapping("/class")
public class ClassController {
	
	@Autowired
	private ClassMappers classMapper;// 班级

	@Autowired
	private StudentService studentService;

	@Autowired
	private StudentDao studentDao;
	
	@Autowired
	private ClassDao classDao;
	
	@Autowired
	private UserInfoDao userinfoDao;
	
	// 查询所有班级
		@RequestMapping("/list")
		@ResponseBody
		public Map<String, Object> index(ClassEntity classEntity,
				@RequestParam(name = "page", defaultValue = "1") Integer pageNum,
				@RequestParam(name = "rows", defaultValue = "20") Integer pageSize) {
			// 查询数据
			PageHelper.startPage(pageNum, pageSize);
			System.out.println("classEntityAAA@#$$:"+classEntity);
			List<ClassEntity> list = classMapper.classList(classEntity);
			PageInfo<ClassEntity> info = new PageInfo<>(list);
			// 组装返回结果
			Map<String, Object> obj = new HashMap<String, Object>();
			obj.put("total", info.getTotal());
			obj.put("rows", info.getList());
			return obj;
		}
		//将该学生的状态由在读中改成就业中
		@GetMapping("biye/{id}")
		public Map<String, Object> biye(@PathVariable(name="id") Long id) {
			System.out.println("进入班级classconroller毕业");
			System.out.println(id);
			Map<String, Object> obj=new HashMap<String, Object>();
			obj.put("success", false);
			ClassEntity classEntity=classDao.findOne(id);
			classEntity.setClassState(Integer.toString(3));
			classDao.save(classEntity);
			List<StudentEntity> studentEntity=studentDao.findByClassId(id);
			for (StudentEntity studentEntity2 : studentEntity) {
				studentEntity2.setState(Integer.toString(3));
				studentDao.save(studentEntity2);
			}
			obj.put("success", true);
			return obj;
		}
		

		@PostMapping("save")
		@ResponseBody
		public Map<String, Object> save(@Valid ClassEntity classEntity, HttpServletRequest request, Model model,
				BindingResult br) {
			Map<String, Object> obj = new HashMap<String, Object>();
			obj.put("success", false);
			if (br.hasErrors()) {
				obj.put("message", "校验失败！");
				obj.put("errors", br.getFieldErrors());
			} else {
				// 获取隐藏域章节编号
				String classId = request.getParameter("id");//获取班级的名称
				String className = request.getParameter("className");//获取班级的名称
				String classTeacherName = request.getParameter("classTeacherName");// 获取班主任姓名
				String jiaoyuanName = request.getParameter("jiaoyuanName");// 获取任课教员的姓名
				ClassEntity classEntity2;
				if (classId == null || "".equals(classId)) {
					// 添加
					System.out.println("添加");
					classEntity2 = new ClassEntity();
				} else {
					// 编辑
					System.out.println("编辑");
					classEntity2 = classDao.findOne(Long.valueOf(classId));
					classEntity2.setClassId(Long.valueOf(classId));
				}
				classEntity2.setClassName(className);
				classEntity2.setClassTeacherId(userinfoDao.findOne(Long.valueOf(classTeacherName)));//设置班级中的班主任
				classEntity2.setTeacherId(userinfoDao.findOne(Long.valueOf(jiaoyuanName)));//设置班级中的教员
				classEntity2.setDelete(1);
				classEntity2.setCreationTime(new Date());
				classEntity2.setClassSize(0);
				classEntity2.setClassState(Integer.toString(2));
				classDao.save(classEntity2);
				obj.put("success", true);
			}
			return obj;
		}

}
